package com.dense.kuiniu.floatcabinet.mvp.manager.bord;

import com.kuiniu.serialport.code.EventCode;
import com.kuiniu.serialport.inter.SerialDataObserver;
import com.kuiniu.serialport.manager.SerialControlManager;
import com.kuiniu.serialport.mode.output.BaseModel;
import com.kuiniu.serialport.mode.output.RFID.receive.RFIDModel;
import com.kuiniu.serialport.mode.output.lock.InitOpenStatusModel;
import com.kuiniu.serialport.mode.output.lock.receive.OpenOrCloseModel;
import com.kuiniu.serialport.utils.StartSignCode;

import java.util.HashMap;
import java.util.Map;

import timber.log.Timber;

/**
 * @author Administrator
 * @date 2020/11/17
 */
public class MessageObserver implements SerialDataObserver {
    private Map<Integer, Map<String, Integer>> map = new HashMap<>();
    private MessageCenter mMessageCenter;


    public MessageObserver() {

    }

    public void setMessageCenter(MessageCenter messageCenter) {
        mMessageCenter = messageCenter;
    }

    public void register() {
        SerialControlManager.getInstance().registerObserver(this);
    }

    public void unRegister() {
        SerialControlManager.getInstance().unRegisterObserver(this);
    }

    @Override
    public void serialPortError(int i, String s) {
        //TODO 串口连接报错
    }

    @Override
    public void backFail(int i, short i1, int i2) {

    }

    @Override
    public void backSucess(int start, short eventCode, BaseModel baseModel) {
        if (start == StartSignCode._5BB5) {
            if (eventCode == EventCode.CONTROL_OPEN_OR_CLOSE) {
                Timber.e("开关门指令回调回调=========================================");
                OpenOrCloseModel model = (OpenOrCloseModel) baseModel;
                //打开柜门的指令响应
                mMessageCenter.doorControlOpenMessage(model.getDeviceNumber(), model.getNumber()
                        , model.isOpen());
            } else if (eventCode == EventCode.INFORM_OPEN_OR_CLOSE) {
                Timber.e("门状态变更回调回调=========================================");
                //门状态的指令返回
                InitOpenStatusModel model = (InitOpenStatusModel) baseModel;
                mMessageCenter.doorStatusChange(model.getDeviceNumber(), model.getLockStatus());
            } else if (eventCode == EventCode.INIT_OPEN_OR_CLOSE) {
                Timber.e("门初始化回调回调=========================================");
                InitOpenStatusModel model = (InitOpenStatusModel) baseModel;
                mMessageCenter.doorStatusInit(model.getDeviceNumber(), model.getLockStatus());
            }
        } else if (start == StartSignCode._5A) {
            switch (eventCode) {
                case EventCode.RFID_READ:
                    map.clear();
                    Timber.e("开启读取成功回调=========================================");
                    break;
                case EventCode.RFID_STOP:
                    Timber.e("读取完成回调=========================================");
                    mMessageCenter.inventory(map);
                    map.clear();
                    break;
                case EventCode.RFID_LABLE_READ_NOTIFY:
                    Timber.e("读取rfid回调=========================================");
                    RFIDModel model = (RFIDModel) baseModel;
                    putData(model);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public boolean myEvent(int i, short i1) {
        return true;
    }

    private void putData(RFIDModel model) {
        //默认版号从1开始
        int deviceId = model.getDeviceNumber();
        //转换为用于显示的门号
        int doorNumber = (deviceId - 1) * DoorManager.DOOR_NUMBER_BY_ONE_BORD + model.getNumber();
        String epc = model.getEPC();

        Map<String, Integer> epcMap = map.get(doorNumber);
        if (epcMap == null) {
            epcMap = new HashMap<>();
            map.put(doorNumber, epcMap);
        }
        epcMap.put(epc, 1);
    }

}